﻿package  {
	import flash.geom.Point;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.utils.setInterval;
	import flash.utils.clearInterval;
	
	public class Player {

		protected var score:Number;
		private var hidePlayer:Boolean;
		private var blinkInterval;
		private var blinkCounter:Number;
		public var pos:Point; // kordinat tengah player
		public var velocity:Point;
		public var health:int;
		public const MAX_HEALTH:int = 4;
				
		protected var playerSprite:GameSprite;
		protected var playerAnimFrameNum:int;		
		protected var playerWidth:int;
		protected var playerHeight:int;	
		protected const playerAnimFrameChangeTime:Number = 0.1; // dalam detik
		protected var animElapsedTime; // waktu semenjak ganti frame dalam detik
		protected const playerAnimMaxFrameCount:int = 4; // jumlah banyak frame animasi player
		
		public function Player(playerSprite:GameSprite) {			
			this.playerSprite = playerSprite;			
			health = MAX_HEALTH;
			score = 0;
			playerWidth = 103;
			playerHeight = 309;
			blinkCounter = 0;
		}	
		
		public function initialize() {
			pos = new Point(0,0);
			velocity = new Point(0,0);
			playerAnimFrameNum = 0;
			animElapsedTime = 0;
		}
		
		public function update(elapsedTime:Number) {
			pos.x += velocity.x * elapsedTime;
			pos.y += velocity.y * elapsedTime;
			
			//update skor jika pemain masih hidup
			if(health>0) 
				score+=elapsedTime;
				
			animate(elapsedTime);
		}
		
		protected function animate(elapsedTime:Number) {
			animElapsedTime += elapsedTime;			
			if(animElapsedTime > playerAnimFrameChangeTime ) {
				animElapsedTime -= playerAnimFrameChangeTime ;
				playerAnimFrameNum = (playerAnimFrameNum + 1) % playerAnimMaxFrameCount;								
			}			
		}
		
		public function render(canvasBD:BitmapData, elapsedTime:Number) {
			if(!hidePlayer)
				canvasBD.copyPixels(playerSprite.bitmapData, new Rectangle(playerAnimFrameNum*getWidth(),0,getWidth(), getHeight()), pos.subtract(playerSprite.origin));
		}
		
		public function blinkIn(interval:Number,maxTime:Number) {
			if(!blinkInterval) blinkInterval = setInterval(blinkAction,interval);
			
		}
		
		public function stopBlink() {
			if(blinkInterval) {
				clearInterval(blinkInterval);
				hidePlayer = false;
				blinkInterval = undefined;
			}
		}
		
		private function blinkAction() {
			if(!hidePlayer) hidePlayer = true;
			else hidePlayer = false;
			blinkCounter += 1;
			if(blinkCounter >= 7) {
				blinkCounter = 0;
				stopBlink();
			}
		}
		
		public function getWidth():Number {
			return playerWidth;			
		}
		
		public function getHeight():Number {			
			return playerHeight;
		}				
		
		public function getOriginX():Number {
			return pos.x - playerSprite.origin.x;
		}
		
		public function getOriginY():Number {
			return pos.y - playerSprite.origin.y;
		}
		
		public function getScore():Number {
			var multiplier:uint = Math.pow(10, 2); 
			var truncatedVal:Number = Math.round(score*multiplier); 
			
			return truncatedVal/multiplier; 
			return score;			
		}
		
		public function addScore(score:Number) {			
			if(health>0)
				this.score+=score;
		}
	}
	
}
